package com.djhu.springsimple.beans.context.lifecycle;

import com.djhu.springsimple.beans.factory.BaseBeanFactory;

import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @author by zw on 2018/4/16.
 */
public class DefaultLifecycleProcessor implements LifecycleProcessor {
    private BaseBeanFactory baseBeanFactory;

    private Map<String, Lifecycle> beans = new LinkedHashMap<>();
    private volatile boolean running;

    public DefaultLifecycleProcessor(BaseBeanFactory baseBeanFactory) {
        this.baseBeanFactory = baseBeanFactory;
    }

    @Override
    public void onRefresh() throws Exception {
        startBeans();
        running = true;
    }

    private void startBeans() throws Exception {
        String[] beanNames = baseBeanFactory.getbeanNamesfortype(Lifecycle.class);
        for (String beanName:beanNames){
            Lifecycle lifecycle = (Lifecycle) baseBeanFactory.getBean(beanName);
            lifecycle.onStart();
        }
    }
}